/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.proyectos.bean;

import com.proyectos.ejb.AlEjbLocal;
import com.proyectos.model.Alumnos;
import java.io.Serializable;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import org.primefaces.context.RequestContext;

/**
 *
 * @author Rafael
 */
public class CtrAlumnos implements Serializable {

    @EJB
    AlEjbLocal ejb;
    String name;
    List<Alumnos> lista;
    Alumnos activeReg;

    public Alumnos getActiveReg() {
        return activeReg;
    }

    public void setActiveReg(Alumnos activeReg) {
        this.activeReg = activeReg;
    }

    public void edit(BigDecimal id) {
        try {
           // System.out.println("hola desde edit");
            this.activeReg = this.ejb.get(id);
            //this.form.setVisible(true);
            //      this.form.setModal(true);
            Map<String, Object> options = new HashMap<String, Object>();
            options.put("modal", true);
            options.put("draggable", false);
            options.put("resizable", false);
            options.put("contentHeight", 320);
            RequestContext.getCurrentInstance().openDialog("mantAlumnos", options, null);
        } catch (Exception ex) {
            Logger.getLogger(CtrAlumnos.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
         
    public void save(ActionEvent event) {
         System.out.println("*****************DENTRO**************");
        try {
           
            this.ejb.save(activeReg);
        } catch (Exception ex) {
            Logger.getLogger(CtrAlumnos.class.getName()).log(Level.SEVERE, null, ex);
        }
        RequestContext.getCurrentInstance().closeDialog(null);
    }
    public void delete(ActionEvent event) {
       
        try {
              //System.out.println(this.ejb.get(BigDecimal.ONE));
            this.ejb.delete(activeReg);
        } catch (Exception ex) {
            Logger.getLogger(CtrAlumnos.class.getName()).log(Level.SEVERE, null, ex);
             System.out.println("***************pp*****************");
        }
        //RequestContext.getCurrentInstance().closeDialog(null);
    }
    
    public List<Alumnos> getLista() {
        return lista;
    }

    public void setLista(List<Alumnos> lista) {
        this.lista = lista;
    }

    public void addMessage(String summary) {
        FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, summary, null);
        FacesContext.getCurrentInstance().addMessage(null, message);
    }

    public void buscar() {
      
                
        TreeMap vparametros = new TreeMap();
        if (name != null && !name.isEmpty()) {
            vparametros.put("cnombre", "%" + name.toUpperCase() + "%");
        } else {
            vparametros.put("cnombre", "%");
        }
        lista = this.ejb.query("Alumnos.findByName", vparametros);

    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public CtrAlumnos() {
    }

}
